package com.workcell.workdata.im.component.observer;

import com.workcell.workdata.im.component.event.BucketCreatedEvent;
import com.workcell.workdata.im.constant.FileBucket;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.Objects;

@Component
public class BucketCreatedObserver {

    @EventListener
    public void moveDefaultUserLogo(BucketCreatedEvent event) throws IOException {

        File defaultUserLogo = new File(event.getBaseBucketFile(), FileBucket.USER_LOGO.getValue() + FileSystems.getDefault().getSeparator() + FileBucket.USER_LOGO.getDefaultFilename());

        FileUtils.writeByteArrayToFile(defaultUserLogo, IOUtils.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/logo/user/default.jpg"))));

    }

}
